import * as vscode from 'vscode';

export enum ErrorType {
  SYNTAX_ERROR = 'syntax',
  TYPE_ERROR = 'type',
  IMPORT_ERROR = 'import',
  UNDEFINED_ERROR = 'undefined',
  FUNCTION_ERROR = 'function',
  VARIABLE_ERROR = 'variable',
  ATTRIBUTE_ERROR = 'attribute',
  SPELLING_ERROR = 'spelling',
  OTHER = 'other'
}

export interface ClassifiedError {
  type: ErrorType;
  category: string;
  priority: number;
  needsLSPSuggestion: boolean;
}

export class ErrorClassifier {
  private static readonly ERROR_PATTERNS: Record<ErrorType, RegExp[]> = {
    [ErrorType.SYNTAX_ERROR]: [
      /syntax/i,
      /unexpected token/i,
      /invalid syntax/i,
      /missing.*semicolon/i,
      /unterminated/i
    ],
    [ErrorType.TYPE_ERROR]: [
      /convert/i, 
      /type.*error/i,
      /cannot.*assign/i,
      /incompatible.*type/i,
      /expected.*got/i,
      /argument.*type/i
    ],
    [ErrorType.IMPORT_ERROR]: [
      /cannot.*import/i,
      /module.*not.*found/i,
      /import.*error/i,
      /no.*module.*named/i,
      /cannot.*resolve/i
    ],
    [ErrorType.UNDEFINED_ERROR]: [
      /undefined/i,
      /not.*defined/i,
      /undeclared/i,
      /cannot.*find/i,
      /does.*not.*exist/i
    ],
    [ErrorType.FUNCTION_ERROR]: [
      /function.*not.*found/i,
      /too.*many.*arguments/i,
      /missing.*arguments/i,
      /invalid.*arguments/i,
      /signature.*mismatch/i
    ],
    [ErrorType.VARIABLE_ERROR]: [
      /variable.*not.*found/i,
      /uninitialized/i,
      /used.*before.*assignment/i
    ],
    [ErrorType.ATTRIBUTE_ERROR]: [
      /不是.*的已知属性/i,
      /has no attribute/i,
      /no such attribute/i,
      /unknown attribute/i,
      /attribute.*not.*found/i
    ],
    [ErrorType.SPELLING_ERROR]: [
      /did you mean/i,
      /未定义.*建议/i,
      /拼写/i,
      /spelling/i
    ],
    [ErrorType.OTHER]: []
  };

  static classify(diagnostic: vscode.Diagnostic): ClassifiedError {
    const message = diagnostic.message.toLowerCase();
    
    for (const [errorType, patterns] of Object.entries(this.ERROR_PATTERNS)) {
      for (const pattern of patterns) {
        if (pattern.test(message)) {
          return {
            type: errorType as ErrorType,
            category: this.getCategoryName(errorType as ErrorType),
            priority: this.getPriority(errorType as ErrorType),
            needsLSPSuggestion: this.needsLSPSuggestion(errorType as ErrorType)
          };
        }
      }
    }

    return {
      type: ErrorType.OTHER,
      category: '其他错误',
      priority: 3,
      needsLSPSuggestion: true
    };
  }

  private static getCategoryName(type: ErrorType): string {
    const names: Record<ErrorType, string> = {
      [ErrorType.SYNTAX_ERROR]: '语法错误',
      [ErrorType.TYPE_ERROR]: '类型错误',
      [ErrorType.IMPORT_ERROR]: '导入错误',
      [ErrorType.UNDEFINED_ERROR]: '未定义错误',
      [ErrorType.FUNCTION_ERROR]: '函数调用错误',
      [ErrorType.VARIABLE_ERROR]: '变量错误',
      [ErrorType.ATTRIBUTE_ERROR]: '属性错误',
      [ErrorType.SPELLING_ERROR]: '拼写错误',
      [ErrorType.OTHER]: '其他错误'
    };
    return names[type];
  }

  private static getPriority(type: ErrorType): number {
    const priorities: Record<ErrorType, number> = {
      [ErrorType.SYNTAX_ERROR]: 1,
      [ErrorType.IMPORT_ERROR]: 1,
      [ErrorType.TYPE_ERROR]: 2,
      [ErrorType.UNDEFINED_ERROR]: 2,
      [ErrorType.FUNCTION_ERROR]: 2,
      [ErrorType.ATTRIBUTE_ERROR]: 2,
      [ErrorType.SPELLING_ERROR]: 2,
      [ErrorType.VARIABLE_ERROR]: 3,
      [ErrorType.OTHER]: 3
    };
    return priorities[type];
  }

  private static needsLSPSuggestion(type: ErrorType): boolean {
    // 语法错误通常不需要LSP建议，其他错误需要
    return type !== ErrorType.SYNTAX_ERROR;
  }
}